import { bexBackground } from 'quasar/wrappers'

// function openExtension (tab) {
//   chrome.tabs.create(
//     {
//       url: chrome.runtime.getURL('www/index.html')
//     },
//     (/* newTab */) => {
//       // Tab opened.
//     }
//   )
// }

// chrome.runtime.onInstalled.addListener(openExtension)
// chrome.action.onClicked.addListener(openExtension)
chrome.action.onClicked.addListener((tab) => {
  sidebarTabId = chrome.sidePanel.open({ windowId: tab.windowId });
  console.log('======')
})

export default bexBackground((bridge /* , allActiveConnections */) => {

  chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
    console.log('===background.js重新加载时执行')
  })
  // 在新的标签也中打开
  bridge.on('open_in_new', ({ data, respond }) => {

    let open_url = chrome.runtime.getURL('www/index.html')
    // 远程连接
    if(data && data.url){
      if(data.url.startsWith('https://') || data.url.startsWith('http://') ){
        open_url = data.url
      } else if(data.url.startsWith('www/')){
        // 本地相对链接
        open_url = chrome.runtime.getURL(data.url)
        
      }
    }
    
    chrome.tabs.create(
      {
        url: open_url
      },
      (/* newTab */) => {
        // Tab opened.
      }
    )
    respond()
  })

  bridge.on('page_add_btn', ({ respond }) => {
    // 调用content-script.js
    console.log('---page_add_btn')
    bridge.send('page_add_btn')
    respond('data-from-backgruond')
  })

  bridge.on('log', ({ data, respond }) => {
    console.log(`[BEX] ${data.message}`, ...(data.data || []))
    respond()
  })


  bridge.on('storage.get', ({ data, respond }) => {
    console.log(data)
    const { key } = data
    if (key === null) {
      chrome.storage.local.get(null, items => {
        // Group the values up into an array to take advantage of the bridge's chunk splitting.
        respond(Object.values(items))
      })
    }
    else {
      chrome.storage.local.get([key], items => {
        respond(items[key])
      })
    }
  })
  // Usage:
  // const { data } = await bridge.send('storage.get', { key: 'someKey' })

  bridge.on('storage.set', ({ data, respond }) => {
    chrome.storage.local.set({ [data.key]: data.value }, () => {
      respond()
    })
  })
  // Usage:
  // await bridge.send('storage.set', { key: 'someKey', value: 'someValue' })

  bridge.on('storage.remove', ({ data, respond }) => {
    chrome.storage.local.remove(data.key, () => {
      respond()
    })
  })
  // Usage:
  // await bridge.send('storage.remove', { key: 'someKey' })

  /*
  // EXAMPLES
  // Listen to a message from the client
  bridge.on('test', d => {
    console.log(d)
  })

  // Send a message to the client based on something happening.
  chrome.tabs.onCreated.addListener(tab => {
    bridge.send('browserTabCreated', { tab })
  })

  // Send a message to the client based on something happening.
  chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
    if (changeInfo.url) {
      bridge.send('browserTabUpdated', { tab, changeInfo })
    }
  })
   */
})
